package com.jx.lcs.feedbackinfo.web;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tianyu.jty.common.persistence.Page;
import com.tianyu.jty.common.persistence.PropertyFilter;
import com.tianyu.jty.common.web.BaseController;
import com.jx.lcs.employee.service.EmployeeService;
import com.jx.lcs.feedbackinfo.entity.FeedBackInfo;
import com.jx.lcs.feedbackinfo.service.FeedBackInfoService;

/**
 * 商品类型类型controller
 * @author ty
 * @date 2015年1月22日
 */
@Controller
@RequestMapping("feedback")
public class FeedBackInfoController extends BaseController{

	private static final Logger logger = LoggerFactory.getLogger(FeedBackInfoController.class);
	@Autowired
	private FeedBackInfoService feedBackInfoService;
	@Autowired
	private EmployeeService employeeService;

	/**
	 * 默认页面
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "feedback/feedBackInfoList";
	}
	
	/**
	 * 获取商品类型类型json
	 */
	@RequestMapping(value="json",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> feedBackInfoList(HttpServletRequest request) {
		
		Page<FeedBackInfo> page=null;
		try {
			page = getPage(request);
			List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);			
			page = feedBackInfoService.search(page, filters);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return getEasyUIData(page);
	}
	
	/**
	 * 添加商品类型跳转
	 * 
	 * @param model
	 */
	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("feedBackInfo",new FeedBackInfo());
		model.addAttribute("action", "create");
		return "feedback/feedBackInfoForm";
	}

	/**
	 * 添加商品类型
	 * 
	 * @param productType
	 * @param model
	 */
	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ResponseBody
	public String create(@Valid FeedBackInfo feedBackInfo, Model model) {
		try{
			feedBackInfo.setId(UUID.randomUUID().toString());
			feedBackInfoService.save(feedBackInfo);
		}catch(Exception e){
			logger.error("--添加产品出现异常！！", e);
		}
		return "success";
	}

	/**
	 * 修改商品类型跳转
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") String id, Model model) {
		model.addAttribute("feedBackInfo", feedBackInfoService.get(id));
		model.addAttribute("action", "update");
		return "feedback/feedBackInfoForm";
	}

	/**
	 * 修改商品类型
	 * 
	 * @param feedBackInfo
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public String update(@Valid @ModelAttribute @RequestBody FeedBackInfo feedBackInfo,Model model) {
		feedBackInfoService.update(feedBackInfo);
		return "success";
	}

	/**
	 * 删除商品类型
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "delete/{id}")
	@ResponseBody
	public String delete(@PathVariable("id") String id) {
		feedBackInfoService.delete(id);
		return "success";
	}
	
	@ModelAttribute
	public void getFeedBackInfo(@RequestParam(value = "id", defaultValue = "-1") String id,Model model) {
		if (id.equals("")) {
			model.addAttribute("feedBackInfo", feedBackInfoService.get(id));
		}
	}
	/**
	 * 根据理财师姓名查询反馈信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "select")
	@ResponseBody
	public Map<String, Object> select(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			String userName = request.getParameter("userName");
			Page<FeedBackInfo> page=null;
			page = getPage(request);
			if(userName!=null && !userName.equals("")){
				
				userName = URLDecoder.decode(userName,"utf-8");
			}
			List<FeedBackInfo> list = feedBackInfoService.getFeedBackInfoByName(userName,page);
			int count = feedBackInfoService.getFeedBackInfoNum(userName);
			map.put("total", count);
			map.put("rows", list);
		
		}catch(Exception e){
			logger.error("--根据理财师姓名查询反馈信息异常！！", e);
		}
		return map;
		
	}

}
